package com.cyan.app.workflow.activity.WHM;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cyan.app.workflow.R;
import com.cyan.app.workflow.base.activity.BaseActivity;
import com.cyan.app.workflow.bean.Invbat2LabBean;
import com.cyan.app.workflow.customview.PullDownView;
import com.cyan.app.workflow.util.AppUtils;

import java.util.ArrayList;

/**
 * 区域库位调拨-入口
 * Created by DXW on 2016-07-07.
 */
public class AreaDbActivity extends BaseActivity implements PullDownView.OnPullDownListener{
    public static final String TAG = "AreaDbActivity";
    private Button mButtonDelete;
    private Button mButtonSub;
    public ArrayList<Invbat2LabBean.Invbat2Bean> mInvbat2BeanList = new ArrayList<Invbat2LabBean.Invbat2Bean>() ;

    //private MannoLzAdapter adapter;
    private ListView areaDbinfo_lv;
    private PullDownView mPullDownView;
    private AreaDbAdapter adapter;
    private int selectedCount = 0;
    private boolean[] checks; //用于保存checkBox的选择状态
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.whm_area_db);
        initView();
        //删除
        mButtonDelete = (Button)findViewById(R.id.whm_area_db_btn_todelete);
        mButtonDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectedCount == 0) {
                    AppUtils.showTip(AreaDbActivity.this, "选择你要操作的记录");
                } else {
                    for (int i = mInvbat2BeanList.size() - 1; i >= 0; i--) {
                        if (checks[i]) {
                            mInvbat2BeanList.remove(i);
                        }
                    }
                    adapter.notifyDataSetChanged();
                }
            }
        });
        //调拨
        mButtonSub = (Button)findViewById(R.id.whm_area_db_btn_db);
        mButtonSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mInvbat2BeanList == null || mInvbat2BeanList.size() == 0) {
                    AppUtils.showTip(AreaDbActivity.this, "沒有可调拨的品号");
                } else {
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("area_db_db",mInvbat2BeanList);
                    intent.putExtras(bundle);
                    intent.setClass(AreaDbActivity.this, AreaDbSubActivity.class);
                    startActivityForResult(intent, 0x0002);
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //新增
        if(requestCode==0x0001 && resultCode==RESULT_OK){
            if(data!=null){
                Invbat2LabBean result = (Invbat2LabBean)data.getSerializableExtra("area_db_new");
                for (Invbat2LabBean.Invbat2Bean bean:result.getResult()){
                    mInvbat2BeanList.add(bean);
                }
                adapter.notifyDataSetChanged();
            }
        }
        if(requestCode==0x0002 && resultCode==RESULT_OK){
            mInvbat2BeanList.clear();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        adapter.notifyDataSetChanged();
    }

    private  void  initView()
    {
        titleBar();
        mPullDownView = (PullDownView) findViewById(R.id.whm_area_db_lv);

        mPullDownView.setOnPullDownListener(this);

        areaDbinfo_lv = mPullDownView.getListView();
        // 隐藏并且禁用头部刷新
        mPullDownView.setHideHeader();
        // 隐藏 并禁用尾部
        mPullDownView.setHideFooter();

        adapter = new AreaDbAdapter(AreaDbActivity.this);
        areaDbinfo_lv.setAdapter(adapter);
    }
    private void titleBar() {
        TextView  mTextViewTitle =(TextView)findViewById(R.id.top_title_info);
        mTextViewTitle.setText("现场库位调拨");
        Button back = (Button) findViewById(R.id.top_title_back);
        back.getBackground().setAlpha(255);// 透明度0~255透明度值 ，值越小越透明
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回按钮
                finish();
            }
        });
        Button mButton = (Button)findViewById(R.id.top_title_refres_with_icon);
        mButton.setVisibility(View.VISIBLE);
        mButton.setText("添加");
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(AreaDbActivity.this, AreaDbNewActivity.class);
                //startActivity(intent);
                startActivityForResult(intent, 0x0001);
            }
        });

        RelativeLayout mRelativeLayout =(RelativeLayout) findViewById(R.id.whm_area_db_foot);
        mRelativeLayout.setVisibility(View.VISIBLE);
    }
    /**
     * listview刷新
     */
    public void onRefresh() {

    }

    /**
     * listview更多
     */
    public void onMore() {
        return;
    }
    public class AreaDbAdapter extends BaseAdapter{
        private LayoutInflater inflater= null;

        public AreaDbAdapter(Context context) {
            inflater=LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            checks =  new boolean[mInvbat2BeanList.size()];
            return mInvbat2BeanList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            holder = new ViewHolder();
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.gravity = Gravity.CENTER_VERTICAL;
            convertView = inflater.inflate(R.layout.whm_area_db_list, null);
            holder.itnbr_bat_itnbr = (TextView) convertView.findViewById(R.id.whm_area_db_list_itnbr);
            holder.itnbr_bat_itnbr.setText(mInvbat2BeanList.get(position).getItnbr());

            holder.itnbr_bat_itdsc = (TextView) convertView.findViewById(R.id.whm_area_db_list_itdsc);
            holder.itnbr_bat_itdsc.setText(mInvbat2BeanList.get(position).getItdsc());

            holder.itnbr_bat_onhand1 = (TextView) convertView.findViewById(R.id.whm_area_db_list_onhand1);
            holder.itnbr_bat_onhand1.setText(mInvbat2BeanList.get(position).getOnhand1().toString());

            holder.itnbr_bat_fixnr = (TextView) convertView.findViewById(R.id.whm_area_db_list_fixnr);
            holder.itnbr_bat_fixnr.setText(mInvbat2BeanList.get(position).getFixnrName());

            holder.itnbr_bat_selected = (CheckBox) convertView.findViewById(R.id.whm_area_db_list_selected);
            final int pos = position;
            holder.itnbr_bat_selected.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        selectedCount++;
                    } else {
                        selectedCount--;
                    }
                    checks[pos] = isChecked;
                }
            });

            holder.itnbr_bat_selected.setChecked(checks[position]);
            convertView.setTag(holder);
            return convertView;
        }
        class ViewHolder {
            CheckBox itnbr_bat_selected;
            TextView itnbr_bat_itnbr;
            TextView itnbr_bat_itdsc;
            TextView itnbr_bat_onhand1;
            TextView itnbr_bat_fixnr;
        }
    }
}
